function queryDeptAddress(deptid, then) {
    $.ajax({
        type: 'POST',
        url: "tag_detail.jsp",
        data: {
            act: "addressQuery",
            deptId: deptid + "",
            hospital: HOSPITAL,
        },
        success: function(res) {
            res = JSON.parse(res);
            if (res.errcode == 0) {
                then(res);
            } else {

            }
        },
    });
}
var state = {
    deptList: [],
    choosedDept: 0,
    all: {
        id: -1,
        checked: false,
    },
    personList: [],
    tagList: [],
    choosedPersonList: [],
    searchInputShow: false,
    searchResult: {},
    timeout: {},
}
var addMember = {
    template: "#tag_add_member",
    props: [],
    data: function() {
        return state;
    },
    computed: {
        // 所有被选中的科室，如果子科室都被选中，就不显示子科室，只选中父科室
        choosedDeptList: function() {
            var deptList = [];
            function recursion(list) {
                if (list.checked) {
                    deptList.push(list);
                    return;
                }
                if (typeof list == "object") {
                    if (Array.isArray(list)) {
                        list.forEach(recursion);
                    } else if (list.sub_dept){
                        list.sub_dept.forEach(recursion);
                    }
                }
            }
            if (this.deptList.length > 0) {
                recursion(this.deptList);
                return deptList;
            }
        },
        hasResult: function() {
            if (this.searchResult.user && this.searchResult.user.length > 0) {
                return true;
            }
            if (this.searchResult.dept && this.searchResult.dept.length > 0) {
                return true;
            }
            if (this.searchResult.label && this.searchResult.label.length > 0) {
                return true;
            }
            return false;
        }
    },
    methods: {
        // 根据id查询科室
        searchDept: function(id) {
            var dept;
            function iterate(list) {
                if (dept) {
                    return;
                }
                if (Array.isArray(list)) {
                    for (var i = 0; i < list.length; i++) {
                        iterate(list[i]);
                    }
                } else {
                    if (list.id == id) {
                        dept = list;
                    }
                    if (list.sub_dept && list.sub_dept.length > 0) {
                        iterate(list.sub_dept);
                    }
                }
            }
            iterate(this.deptList);
            return dept;
        },
        // 从模糊搜索中选中
        searchChoosed: function(i, name) {
            if (name == "user") {
                this.personCheckChange(i);
            } else if (name == "dept") {
                var dept = this.searchDept(i.id);
                function setChecked(list) {
                    if (Array.isArray(list)) {
                        for (var i = 0; i < list.length; i++) {
                            setChecked(list[i]);
                        }
                    } else {
                        list.checked = true;
                        if (list.sub_dept && list.sub_dept.length > 0) {
                            setChecked(list.sub_dept);
                        }
                    }
                }
                if (dept) {
                    setChecked(dept);
                }
            }
            document.querySelector("._fast-search").value = "";
            this.searchResult = {};
        },
        // 判断科室、人员、标签的搜索结果块是否显示
        showSearchResult: function(key) {
            return this.searchResult[key].length > 0;
        },
        showSearch: function() {
            this.searchInputShow = true;
        },
        hideSearch: function() {
            var _this = this;
            setTimeout(function() {
                _this.searchInputShow = false;
            }, 200)
        },
        // 所有的那个框被点击的时候让input focus
        fuzzyFocus: function() {
            document.querySelector("._fast-search").focus();
        },
        // 模糊搜索
        searchBlur: function(event) {
            var value = event.target.value;
            clearTimeout(this.timeout);
            var _this = this;
            this.timeout = setTimeout(function() {
                if (value == "") {
                    _this.searchResult = {};
                    return;
                }
                $.ajax({
                    type: 'POST',
                    url: location.href,
                    data: {
                        act: "fuzzySearch",
                        hospital: HOSPITAL,
                        key: value,
                    },
                    success: function(res) {
                        res = JSON.parse(res);
                        if (res.errcode == 0) {
                            // 已经被选中的内容就不显示在模糊搜索框上
                            _this.searchResult = {
                                 dept: res.data.dept.filter(function(item) {
                                        for (var i = 0; i < _this.choosedDeptList.length; i++) {
                                            if (item.id == _this.choosedDeptList[i].id) {
                                                return false;
                                            }
                                        }
                                        return true;
                                     }),
                                 user: res.data.user.filter(function(item) {
                                     for (var i = 0; i < _this.choosedPersonList.length; i++) {
                                         if (item.id == _this.choosedPersonList[i].id) {
                                             return false;
                                         }
                                     }
                                     return true;
                                 }),
                            }
                        } else {
                            _this.danger(res.errmsg);
                        }
                    },
                });
            }, 700);
        },
        // 选择全院
        deptQuery: function() {
            var _this = this;
            this.choosedDept = -1;
            queryDeptAddress(-1, function(res) {
                if (res.errcode == 0) {
                    _this.personList = res.data;
                } else {
                    _this.$dispatch("danger", res.errmsg);
                }

            });
        },
        // 改变这个人被选中的事实
        personCheckChange: function(person) {
            if (this.personCheck(person)) {
                this.choosedPersonList = this.choosedPersonList.filter(function(item) {
                    return item.id != person.id;
                });
            } else {
                this.choosedPersonList.push(person);
            }
        },
        // 取消选中科室
        removeDept: function(dept) {
            function setChecked(list) {
                if (Array.isArray(list)) {
                    for (var i = 0; i < list.length; i++) {
                        setChecked(list[i]);
                    }
                } else {
                    list.checked = false;
                    if (list.sub_dept && list.sub_dept.length > 0) {
                        list.toggle = 1;
                        setChecked(list.sub_dept);
                    }
                }
            }
            setChecked(dept);
        },
        // 判断当前这个人是否被选中
        personCheck: function(person) {
            return this.choosedPersonList.filter(function(item) {
                    return item.id == person.id;
                }).length > 0;
        },
        // 关闭这个页面
        closeAddMember: function() {
            this.$dispatch("closeAddMember");
        },
        // 切换选项卡
        changeCard: function(card) {
            this.card = card;
        },
        submit: function() {
            this.$dispatch("addMember", {
                person: this.choosedPersonList,
                dept: this.choosedDeptList,
            });
        },
    },
    events: {
        // 根据科室id查用户
        deptQuery: function(id) {
            var _this = this;
            queryDeptAddress(id, function(res) {
                if (res.errcode == 0) {
                    _this.personList = res.data;
                } else {
                    _this.$dispatch("danger", res.errmsg);
                }
            });
        },
        // 让全院节点能够与category组件同步
        parentRecursion: function() {
            for (var i = 0; i < this.deptList.length; i++) {
                if (!this.deptList[i].checked) {
                    if (this.all) {
                        this.all.checked = false;
                    }
                    return;
                }
            }
            if (this.all) { // 让父节点收到消息并修改自身状态
                this.all.checked = true;
            }
        }
    },
    created: function() {
        state.deptList = [];
        state.choosedDept= 0;
        state.all = {
            id: -1,
            checked: false,
        };
        state.personList = [];
        state.tagList= [];
        state.choosedPersonList = [];
        state.searchResult = {};
    },
    ready: function() {
        var _this = this;
        // 获取科室的组织架构
        $.ajax({
            type: 'POST',
            url: "tag_detail.jsp",
            data: {
                act: "organizationQuery",
                deptType: "20",
                hospital: HOSPITAL
            },
            success: function(res) {
                res = JSON.parse(res);
                if (res.errcode == 0) {
                    var deptList = res.data;
                    function setChecked(list) {
                        if (Array.isArray(list)) {
                            for (var i = 0; i < list.length; i++) {
                                setChecked(list[i]);
                            }
                        } else {
                            list.checked = false;
                            if (list.sub_dept && list.sub_dept.length > 0) {
                                list.toggle = 1;
                                setChecked(list.sub_dept);
                            }
                        }
                    }
                    setChecked(deptList);
                    _this.deptList = deptList;
                } else {
                    _this.$dispatch("danger", res.errmsg);
                }
            },
        });
    },
    components: {
        deptcategory: deptCategory,
    }
}